<!--
 * @Author: wei_jt@ecidi.com
 * @Date: 2019-11-01 15:02:52
 * @LastEditors: chen_yt
 * @LastEditTime: 2022-06-23 15:59:19
 * @Description: 无效路径统一跳转页
 -->
<template>
  <div class="wscn-http404">
    <div class="pic-404">
      <img
        class="pic-404__parent"
        :src="require(`@/assets/img/error/${type}.svg`)"
        :alt="type"
      />
      <img class="pic-404__child left" :src="img_404_cloud" alt="404" />
      <img class="pic-404__child mid" :src="img_404_cloud" alt="404" />
      <img class="pic-404__child right" :src="img_404_cloud" alt="404" />
    </div>
    <div class="text">
      <div class="tone">
        {{ LANG.TONE }}
      </div>
      <div class="desc">
        {{ LANG[`ERROR_${type}`] }}
      </div>
      <div class="tip">
        {{ LANG[`DESC_${type}`] }}
      </div>
      <div class="btn">
        <portal v-if="portals">
          <template v-slot="{ portal }">
            <fks-button class="fks-icon-toggle">
              {{ portal }}
            </fks-button>
          </template>
        </portal>
        <fks-button
          v-if="isSaasMode"
          class="fks-icon-toggle"
          style="margin-left: 10px"
          @click="goTenantSelect"
        >
          {{ LANG.GO_TENANT_SELECT }}
        </fks-button>
        <fks-button type="primary" style="margin-left: 10px" @click="back">
          {{ LANG.GO_BACK }}
        </fks-button>
      </div>
    </div>
  </div>
</template>

<script>
import { IS_SAAS_MODE } from "@/store/Getter/getterTypes.js";
import Portal from "@/components/Portal";
import img_404_cloud from "@/assets/img/error/404_cloud.png";
import store from "@/store";
import { getRoutePath } from "./utils.js";
import storage from "@/utils/storage";
export default {
  components: {
    Portal,
  },
  props: {
    type: {
      default: "404",
    },
  },
  data() {
    return {
      img_404_cloud,
    };
  },
  computed: {
    isSaasMode() {
      return this.$store.getters[IS_SAAS_MODE];
    },
    portals() {
      return this.$store.state.portals.length;
    },
  },
  async beforeRouteEnter(to, from, next) {
    if (storage.get("changePortal") == "1") {
      let routes = store.state.routes;
      let homeFlag = routes.find((item) => {
        return item.code == "home" || item.code == "Home";
      });
      if (!homeFlag && routes.length > 3) {
        let routePath = await getRoutePath(routes);
        storage.remove("changePortal");
        if (routePath) {
          next(routePath);
          return;
        }
        next();
        return;
      } else {
        next();
        return;
      }
    } else {
      next();
    }
  },

  methods: {
    goTenantSelect() {
      this.$router.push("/tenantSelect");
    },
    back() {
      let path = "/home";
      if (this.$storage.get("access_token") == null) {
        path = "/login";
      } else {
        path = "/distribute";
        this.$storage.set("changePortal", 1);
        this.$storage.set("redirectUrl", "/home");
      }
      location.href = location.href.replace("/" + this.type, path);
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.wscn-http404 {
  display: flex;
  align-items: center;
  overflow: hidden;

  img {
    width: 100%;
  }

  .pic-404 {
    position: relative;

    &__child {
      position: absolute;
      &.left {
        width: 80px;
        opacity: 0;
        animation-name: cloudLeft;
        animation-duration: 2s;
        animation-timing-function: linear;
        animation-delay: 1s;
        animation-fill-mode: forwards;
      }

      &.mid {
        width: 46px;
        opacity: 0;
        animation-name: cloudMid;
        animation-duration: 2s;
        animation-timing-function: linear;
        animation-delay: 1.2s;
        animation-fill-mode: forwards;
      }

      &.right {
        width: 62px;
        opacity: 0;
        animation-name: cloudRight;
        animation-duration: 2s;
        animation-timing-function: linear;
        animation-delay: 1s;
        animation-fill-mode: forwards;
      }

      @keyframes cloudLeft {
        0% {
          top: 17px;
          left: 220px;
          opacity: 0;
        }

        20% {
          top: 33px;
          left: 188px;
          opacity: 1;
        }

        80% {
          top: 81px;
          left: 92px;
          opacity: 1;
        }

        100% {
          top: 97px;
          left: 60px;
          opacity: 0;
        }
      }

      @keyframes cloudMid {
        0% {
          top: 10px;
          left: 420px;
          opacity: 0;
        }

        20% {
          top: 40px;
          left: 360px;
          opacity: 1;
        }

        70% {
          top: 130px;
          left: 180px;
          opacity: 1;
        }

        100% {
          top: 160px;
          left: 120px;
          opacity: 0;
        }
      }

      @keyframes cloudRight {
        0% {
          top: 100px;
          left: 500px;
          opacity: 0;
        }

        20% {
          top: 120px;
          left: 460px;
          opacity: 1;
        }

        80% {
          top: 180px;
          left: 340px;
          opacity: 1;
        }

        100% {
          top: 200px;
          left: 300px;
          opacity: 0;
        }
      }
    }
  }

  .text {
    min-width: 500px;
    padding-left: 128px;

    .tone {
      margin-bottom: 20px;
      font-weight: bold;
      font-size: 24px;
      line-height: 32px;
      color: #0b4870;
      opacity: 0;
      animation-name: slide-up;
      animation-duration: 1s;
      animation-fill-mode: forwards;
    }

    .desc {
      margin: 16px 0 32px;
      font-size: 24px;
      line-height: 32px;
      color: #191919;
      opacity: 0;
      animation-name: slide-up;
      animation-duration: 1s;
      animation-delay: 0.1s;
      animation-fill-mode: forwards;
    }

    .tip {
      font-size: 16px;
      line-height: 20px;
      color: #555;
      opacity: 0;
      animation-name: slide-up;
      animation-duration: 1s;
      animation-delay: 0.2s;
      animation-fill-mode: forwards;
    }

    .btn {
      display: flex;
      justify-content: flex-end;
      margin-top: 48px;
      opacity: 0;
      animation-name: slide-up;
      animation-duration: 1s;
      animation-delay: 0.3s;
      animation-fill-mode: forwards;
    }

    @keyframes slide-up {
      0% {
        opacity: 0;
        transform: translateY(100px);
      }

      100% {
        opacity: 1;
        transform: translateY(0);
      }
    }
  }
}
</style>
